package com.libl.controller;

import com.libl.annotation.RequiresPermissionsDesc;
import com.libl.pojo.DtsAdmin;
import com.libl.service.DtsAdminService;
import com.libl.util.ResponseUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/admin/admin")
public class DtsAdminAdminController {
    @Autowired
    DtsAdminService adminService;

    //shiro的注解：标识该方法需要哪些权限
    @RequiresPermissions("admin:admin:list")
    //自定义注解：用于标识该方法的权限信息
    @RequiresPermissionsDesc(menu = {"系统管理","管理员管理"}, button = "查询")
    @GetMapping("/list")
    public Object list(
            @RequestParam(value = "page",required = false, defaultValue = "1") Integer page,
            @RequestParam(value = "limit",required = false, defaultValue = "20") Integer limit,
            String username , String sort, String order) {

        Map<String,Object> data = adminService.queryPage(page, limit, username, sort, order);
        //把查询到的数据返回
        return ResponseUtil.ok(data);
    }

    @RequiresPermissions("admin:admin:create")
    @RequiresPermissionsDesc(menu = {"系统管理","管理员管理"}, button = "添加")
    @PostMapping("/create")
    public Object create(@RequestBody DtsAdmin admin) {
        admin = adminService.insert(admin);
        //把新增后的对象返回
        return ResponseUtil.ok(admin);
    }

    @RequiresPermissions("admin:admin:update")
    @RequiresPermissionsDesc(menu = {"系统管理","管理员管理"}, button = "编辑")
    @PostMapping("/update")
    public Object update(@RequestBody DtsAdmin admin) {
        admin = adminService.update(admin);
        //把修改后的对象返回
        return ResponseUtil.ok(admin);
    }

    @RequiresPermissions("admin:admin:delete")
    @RequiresPermissionsDesc(menu = {"系统管理","管理员管理"},button = "删除")
    @PostMapping("/delete")
    public Object delete(@RequestBody DtsAdmin admin) {
        try {
            adminService.delete(admin);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return ResponseUtil.fail(-1, e.getMessage());
        }
        return ResponseUtil.ok();
    }

}
